<?php
namespace app\controller;

class IndexController extends BaseController
{
    //项目列表
    public function index()
    {
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("member_id",session("member_id"));
        $this->sitedb->map_eq("status",1);
        $project_ids=$this->sitedb->column("project_member","member_id","project_id");
        $project_ids=array_keys($project_ids)?array_keys($project_ids):[];

        $this->sitedb->map=[];
        if(input("type")){
            if(input("type")=="create"){
                $this->sitedb->map_eq("member_id",session("member_id"));
            }else{
                $this->sitedb->map_in("id",$project_ids);
            }
        }else{
            $this->sitedb->mapOr=[
                ["member_id","=",session("member_id")],
                ['id',"in",$project_ids]
            ];
        }
        if(input("search")){
            $this->sitedb->map_like("title",input("search"));
        }
        $data=$this->sitedb->paginate("project");
        $member_ids=null;
        foreach($data['list'] as $row){
            $member_ids[]=$row['member_id'];
        }
        $member_data=null;
        if($member_ids){
            $this->sitedb->map=[];
            $this->sitedb->map_in("id",$member_ids);
            $this->sitedb->mapOr=null;
            $member_data=$this->sitedb->column("member","nickname","id");
        }
		foreach($data['list'] as &$row){
			$row['nickname']=$member_data[$row['member_id']];
		}
		return view('', $data);
    }
    //项目详情
    public function detail()
    {
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("id",input('id'));
        $data['info']=$this->sitedb->find("project");

        $this->sitedb->map=[];
        $this->sitedb->map_eq("project_id",input('id'));
        $this->sitedb->field='id,project_id,member_id,update_time';
        $project_member=$this->sitedb->select("project_member");
        $data['project_member']=$project_member;
        $project_member_ids[]=$data['info']['member_id'];
        foreach($project_member as $row){
            $project_member_ids[]=$row['member_id'];
        }
        if(!in_array(session("member_id"),$project_member_ids)){
            //return $this->error("只能查看自己的项目","",url("Index/index"));
        }
        $this->sitedb->map=[];
        $this->sitedb->map_in("id",$project_member_ids);
        $this->sitedb->field='id,nickname';
        $data['member_data']=$this->sitedb->column("member","nickname","id");
        return view('', $data);
    }
    //添加项目
    public function add(){
        if(request()->isPost()){
            return $this->update();
        }else{
            return $this->operate();
        }
    }
    //修改项目
    public function edit(){
        if(request()->isPost()){
            return $this->update();
        }else{
            return $this->operate();
        }
    }
    //通用项目编辑展示方法
    private function operate(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        $data['info']=null;
        if(input("id")){
            $this->sitedb->map=[];
            $this->sitedb->map_eq("id",input('id'));
            $data['info']=$this->sitedb->find("project");
            if($data['info']['member_id']!=session("member_id")){
                return $this->error("只有项目创建人可以修改项目信息","",url("Index/index"));
            }
        }
        return view('operate',$data);
    }
    //通用项目编辑提交方法
    private function update(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        $this->sitedb->map=[];
        $data=[
            'title'             => input("title"),
            'project_url'       => input("project_url"),
            'project_inside_url'=> input("project_inside_url"),
            'remark'            => input("remark")
        ];
        if(input("id")) {
            $this->sitedb->map_eq("id", input('id'));
            $project_info=$this->sitedb->find("project");
            if($project_info['member_id']!=session("member_id")){
                return $this->error("只有项目创建人可以修改项目信息");
            }
            $data['id']=input("id");
        }else{
            $data['unique_code']=time().$this->stroperate->getRandChar(6);
            $data['member_id']=session("member_id");
        }
        /* 验证 -S */
        $validate=new \app\validate\Project;
        $check_result=$validate->check($data);
        if(!$check_result){
            return $this->error($validate->getError());
        }
        /* 验证 -E */
        $this->sitedb->data=$data;
        $result = $this->sitedb->update("project");
        return $this->success("项目创建成功","",url("Index/detail",['id'=>(input("id")?input("id"):$result)]));
    }
    //退出项目
    public function quit(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        if(!input("project_id")){
            return $this->error("项目ID异常");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("id",input("project_id"));
        $this->sitedb->map_eq("member_id",session("member_id"));
        $result = $this->sitedb->delete_data("project_member");
        return $this->success("退出成功","",url("Index/index"));
    }
    //移除项目成员
    public function remove(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        if(!input("project_id")){
            return $this->error("项目ID异常");
        }
        if(!input("member_id")){
            return $this->error("用户ID异常");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("id",input('project_id'));
        $project_info=$this->sitedb->find("project");
        if($project_info['member_id']!=session("member_id")){
            return $this->error("只有项目创建人可以移除成员");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("project_id",input("project_id"));
        $this->sitedb->map_eq("member_id",input("member_id"));
        $result = $this->sitedb->delete_data("project_member");
        return $this->success("移除成功","",url("Index/detail",['id'=>input('project_id')]));
    }
    //搜索用户
    public function search_member(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        if(!input("project_id")){
            return $this->error("项目ID异常");
        }
        if(!input("search")){
            return $this->error("请输入用户名或手机号码");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("id",input('project_id'));
        $project_info=$this->sitedb->find("project");
        if($project_info['member_id']!=session("member_id")){
            return $this->error("只有项目创建人可以处理加入申请");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("project_id",input("project_id"));
        $project_member = $this->sitedb->column("project_member","id","member_id");
        $project_member_ids=array_keys($project_member);
        $this->sitedb->map=[];
        $this->sitedb->map_like("nickname|mobile",input("search"));
        $this->sitedb->map_neq("id",$project_info['member_id']);
        $member_result = $this->sitedb->select("member");
        $member_data=null;
        foreach ($member_result as &$row){
            if(in_array($row['id'],$project_member_ids)){
                $row['is_join']=1;
            }else{
                $row['is_join']=0;
            }
            $member_data[]=[
                'id'=>$row['id'],
                'is_join'=>$row['is_join'],
                'nickname'=>$row['nickname'],
                'mobile'=>$row['mobile']
            ];
        }
        return $this->success("成员搜索",$member_data);
    }
    //添加用户到项目
    public function add_member(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        if(!input("project_id")){
            return $this->error("项目ID异常");
        }
        if(!input("member_id")){
            return $this->error("用户ID异常");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("id",input('project_id'));
        $project_info=$this->sitedb->find("project");
        if($project_info['member_id']!=session("member_id")){
            return $this->error("只有项目创建人可以添加成员");
        }
        $this->sitedb->map=[];

        $this->sitedb->map_eq("project_id",input('project_id'));
        $this->sitedb->map_eq("member_id",input('member_id'));
        $project_member_count=$this->sitedb->count_data("project_member");
        if($project_member_count==0){
            $this->sitedb->map=[];
        }
        $this->sitedb->data=[
            "project_id"=>input("project_id"),
            "member_id"=>input("member_id"),
            "status"=>1,
        ];
        $result = $this->sitedb->update("project_member");
        if(!is_numeric($result)){
            return $this->error($result);
        }
        return $this->success("成员添加成功");
    }
    //删除项目
    public function del(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        if(!input("project_id")){
            return $this->error("项目ID异常");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("id",input('project_id'));
        $project_info=$this->sitedb->find("project");
        if($project_info['member_id']!=session("member_id")){
            return $this->error("只能删除自己创建的项目");
        }
        $this->sitedb->delete_data("project");
        $this->sitedb->map=[];
        $this->sitedb->map_eq("project_id",input('project_id'));
        $this->sitedb->delete_data("project_member");
        $this->sitedb->delete_data("project_api");
        return $this->success("删除成功","",url("Index/index"));
    }
    //重新生成项目外部访问地址
    public function generate(){
        if(!session("member_id")){
            return redirect(url('Account/login'));
        }
        if(!input("project_id")){
            return $this->error("项目ID异常");
        }
        $this->sitedb->map=[];
        $this->sitedb->map_eq("id",input('project_id'));
        $project_info=$this->sitedb->find("project");
        if(session("member_id")!=$project_info['member_id']){
            $this->error("只有项目创建人可以重新生成外部访问地址");
        }
        $unique_code=time().$this->stroperate->getRandChar(6);
        $this->sitedb->data=[
            'unique_code' => $unique_code
        ];
        $result = $this->sitedb->update("project");
        return $this->success($unique_code);
    }
}
